//导入 createSlice 函数
import {createSlice} from '@reduxjs/toolkit';

//创建 slice 对象
let slice = createSlice({
  name: 'zan',
  initialState: { //建议使用对象作为状态的值
    value: 0
  },
  reducers: {
    add: (state, action) => {
      // immer
      //如果状态值为引用类型的数据, 可以直接对其进行编辑, redux 会根据编辑后的值创建一个新的值, 用来更新内部的状态
      state.value += action.payload;
    },
    sub: (state, action) => {
      state.value -= action.payload;
    }
  }
});

//获取action creator
export let {add, sub} = slice.actions;

//获取 reducer 函数
let reducer = slice.reducer;

//暴露
export default reducer;